// API Key测试脚本
const https = require('https');

// 测试DeepSeek API Key
function testDeepSeekAPI() {
    console.log('🔑 测试DeepSeek API Key...');
    
    const apiKey = 'hKTj5Z46h-BKXumnyr1Apl6KX3zB6F8E3MZcUhpQ';
    const baseUrl = 'https://api.qnaigc.com';
    const model = 'deepseek-v3';
    
    const postData = JSON.stringify({
        model: model,
        messages: [
            {
                role: 'system',
                content: 'You are a helpful assistant.'
            },
            {
                role: 'user',
                content: 'Hello, please introduce yourself.'
            }
        ],
        temperature: 0.7,
        max_tokens: 100
    });
    
    const url = new URL('/v1/chat/completions', baseUrl);
    
    const options = {
        hostname: url.hostname,
        port: 443,
        path: url.pathname,
        method: 'POST',
        headers: {
            'Authorization': `Bearer ${apiKey}`,
            'Content-Type': 'application/json',
            'Content-Length': Buffer.byteLength(postData)
        }
    };
    
    console.log('📡 发送请求到:', baseUrl + '/v1/chat/completions');
    console.log('🔑 使用API Key:', apiKey.substring(0, 10) + '...');
    console.log('🤖 使用模型:', model);
    
    const req = https.request(options, (res) => {
        console.log(`\n📊 响应状态码: ${res.statusCode}`);
        console.log('📋 响应头:', res.headers);
        
        let data = '';
        res.on('data', (chunk) => {
            data += chunk;
        });
        
        res.on('end', () => {
            console.log('\n📝 响应内容:');
            console.log(data);
            
            try {
                const response = JSON.parse(data);
                if (response.choices && response.choices[0] && response.choices[0].message) {
                    console.log('\n✅ API调用成功！');
                    console.log('🤖 AI回复:', response.choices[0].message.content);
                } else if (response.error) {
                    console.log('\n❌ API调用失败:');
                    console.log('错误类型:', response.error.type);
                    console.log('错误消息:', response.error.message);
                } else {
                    console.log('\n⚠️ 未知响应格式');
                }
            } catch (e) {
                console.log('\n❌ 响应解析失败:', e.message);
            }
        });
    });
    
    req.on('error', (e) => {
        console.error(`\n❌ 请求错误: ${e.message}`);
    });
    
    req.write(postData);
    req.end();
}

// 运行测试
console.log('🚀 开始API Key测试...\n');
testDeepSeekAPI();